package com.relax;


import com.relax.bean.Person;
import com.relax.bean.RainBow;
import com.relax.circularDependency.BService;
import com.relax.config.MainConfig;
import com.relax.config.MainConfig2;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

public class SpringApplicationDemo {
    public static void main(String[] args) {
//        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//        Person person = (Person) context.getBean("person");
//        System.out.println(person.getName());
        /**
         * 使用注解方法
         */
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
        Person p = (Person) context.getBean("person1");
        System.out.println(p);
        System.out.println("--------------");

        String[] beanNamesForType = context.getBeanNamesForType(Person.class);
        for (String name : beanNamesForType) {
            System.out.println(name);

        }
    }

    @Test
    public void testIoc() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
        //查看容器中所有的bean
        String[] beanDefinitionNames = context.getBeanDefinitionNames();
        for (String name : beanDefinitionNames) {
            System.out.println(name);

        }
    }

    @Test
    public void testFactoryBean() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig2.class);
        //查看容器中所有的bean
        String[] beanDefinitionNames = context.getBeanDefinitionNames();
        for (String name : beanDefinitionNames) {
            System.out.println("beanDefinitionNames==>" + name);
        }
        System.out.println("======================================================================");
        Object color1 = context.getBean("colorFactoryBean");
        System.out.println("第一次从colorFactoryBean拿出来的数据为:" + color1);//获取 的是color对象

        Object color2 = context.getBean("colorFactoryBean");
        System.out.println("第一次从colorFactoryBean拿出来的数据为:" + color2);//获取 的是color对象

        Object colorFactoryBean2 = context.getBean("&colorFactoryBean");//获取的是colorFactoryBean
        System.out.println("colorFactoryBean本身是:" + colorFactoryBean2);

        System.out.println("======================================================================");
        Object blue = context.getBean("blue");
        System.out.println(blue);


        System.out.println("===================================测试bean的多例===================================");
        Person person = (Person) context.getBean("person");
        System.out.println(person);
        Person person2 = (Person) context.getBean("person");
        System.out.println(person2);
    }

    @Test
    public void test03() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig2.class);
        ConfigurableEnvironment environment = context.getEnvironment();
        String property = environment.getProperty("os.name");
        System.out.println(property);
    }

    /**
     * 循环依赖测试
     */
    @Test
    public void testCircularDependency() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig2.class);
        String[] beanDefinitionNames = context.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {
            System.out.println("=========" + beanDefinitionName);
        }
        BService bService = context.getBean(BService.class);
        bService.methodB();
    }


    /**
     * ImportBeanDefinitionRegistrar  测试
     */
    @Test
    public void testImportBeanDefinitionRegistrar() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig2.class);
        RainBow rainBow = (RainBow) context.getBean("rainBow2");
        rainBow.hello();
    }


    /**
     * 测试自定义statrt
     */
//    @Test
//    public void testMyStart() {
//        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig2.class);
//        MyStartService myStartService = (MyStartService) context.getBean("myStartService");
//        String result = myStartService.whoAreYou("彭青松");
//        System.out.println(result);
//    }

}
